<template>
  <view>
    <!-- 手动添加窗口 -->
    <view class="add" style="margin: 20upx">
      <view class="name">
        <uni-easyinput
          v-model="queryParams.clientName"
          prefixIcon="person"
          placeholder="请输入用户名"
        ></uni-easyinput>
      </view>
      <view class="clientAddress">
        <uni-easyinput
          v-model="queryParams.clientAddress"
          prefixIcon="map-pin-ellipse"
          placeholder="请输入地址"
        >
        </uni-easyinput>
      </view>
      <view class="date">
        <uni-datetime-picker
          v-model="queryParams.date"
          type="date"
          :clear-icon="false"
        />
      </view>
      <!-- <button type="primary" size="mini" style="margin-top: 15upx;" @click="add()">确定</button> -->
      <button
        type="primary"
        size="mini"
        style="margin-top: 15upx"
        @click="determine()"
      >
        添加
      </button>
    </view>

    <!--表格 -->
    <view class="uni-container">
      <uni-table
        ref="table"
        border
        stripe
        type="selection"
        emptyText="暂无更多数据"
        @selection-change="selectionChange"
      >
        <uni-tr>
          <uni-th width="50" align="center">序号</uni-th>
          <uni-th width="150" align="center">日期</uni-th>
          <uni-th width="150" align="center">姓名</uni-th>
          <uni-th align="center">地址</uni-th>
          <uni-th width="204" align="center">设置</uni-th>
        </uni-tr>
        <!-- 遍历这个list把数据呈现在页面上 -->
        <uni-tr v-for="(item, index) in clientList">
          <uni-td align="center">{{ index + 1 }}</uni-td>
          <uni-td>{{ item.date }}</uni-td>
          <uni-td>
            <view class="name">{{ item.clientName }}</view>
          </uni-td>
          <uni-td align="center">{{ item.clientAddress }}</uni-td>
          <uni-td>
            <view class="uni-group">
              <button
                class="uni-button"
                size="mini"
                type="primary"
                @click="edit(item, index)"
              >
                修改
              </button>
              <button
                class="uni-button"
                size="mini"
                type="warn"
                @click="del(index)"
              >
                删除
              </button>
            </view>
          </uni-td>
        </uni-tr>
      </uni-table>
      <view class="uni-pagination-box">
        <uni-pagination
          show-icon
          :page-size="queryParams.pageSize"
          :current="queryParams.pageNum"
          :total="total"
          @change="getList"
        />
      </view>
    </view>
  </view>
</template>

<script>
import {
  listClient,
  getClient,
  delClient,
  addClient,
  updateClient,
} from "@/api/bpm/client";

export default {
  name: "Client",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      clientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientName: null,
        clientAddress: null,
        contactId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      listClient(this.queryParams).then((response) => {
        this.clientList = response.rows;
        this.total = response.total;
        this.loading = false;

        console.log("List got");
        console.log(response);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clientId: null,
        clientName: "",
        clientAddress: "",
        contactId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.clientId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const clientId = row.clientId || this.ids;
      getClient(clientId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.clientId != null) {
            updateClient(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClient(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clientIds = row.clientId || this.ids;
      this.$modal
        .confirm('是否确认删除客户信息编号为"' + clientIds + '"的数据项？')
        .then(function () {
          return delClient(clientIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "bpm/client/export",
        {
          ...this.queryParams,
        },
        `client_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style>
.app-container {
  padding: 20px;
}

.form-item {
  margin-bottom: 10px;
}

button {
  margin-right: 10px;
}

.table {
  margin-top: 20px;
}

.table-header,
.table-row {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  border-bottom: 1px solid #ccc;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}

.dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.dialog form {
  background: white;
  padding: 20px;
  border-radius: 10px;
}
</style>
